package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.OrderInfoBean;
import com.project.bean.WaitProductBean;
import com.project.dto.WaitProductDto;
import com.project.dto.WaitProductDtoWeb;
import com.project.mapper.IOrderInfoMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IWaitProductMapper;
import com.project.service.IWaitProductServiceRE;
import com.project.util.ConvertBeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class WaitProductServiceRE implements IWaitProductServiceRE {
    @Autowired
    private IWaitProductMapper waitProductDao;
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IOrderInfoMapper orderInfoMapper;

    @Override
    public IPage<WaitProductDto> findWaitProduct(int pageNO, String productType, Integer productStatus) {

        QueryWrapper<WaitProductBean> queryWrapper = new QueryWrapper<>();
        if(productType != null && productType.length() != 0 ){
            queryWrapper.like("productType",productStatus);
        }
        if (productStatus!=null){
            queryWrapper.eq("productStatus",productStatus);
        }

        // 构造查询分页参数
        Page<WaitProductBean> page = new Page<>(pageNO, 3);

        IPage<WaitProductBean> waitProductPage = waitProductDao.selectPage(page, queryWrapper);

        // 将查询结果的WaitProductBean转换为WaitProductDto
        List<WaitProductDto> waitProductDtoList = waitProductPage.getRecords().stream().map(bean -> {
            WaitProductDto dto = new WaitProductDto();
            dto.setId(bean.getId());
            dto.setProductName(bean.getProductName());
            dto.setProductType(bean.getProductType());
            dto.setInPrice(bean.getInPrice());
            dto.setProductStatus(bean.getProductStatus() == 0 ? "上架" : "下架");
            dto.setNum(bean.getNum());
            dto.setProductId(bean.getProductId());
            dto.setColor(bean.getColor());
            dto.setSize(bean.getSize());
            return dto;
        }).collect(Collectors.toList());
        // 将转换后的WaitProductDto放到IPage对象中返回
        Page<WaitProductDto> waitProductDtoPage = new Page<>(page.getCurrent(), page.getSize(), waitProductPage.getTotal());
        waitProductDtoPage.setRecords(waitProductDtoList);

        return waitProductDtoPage;
    }


    @Override
    public Boolean updateWaitProductNumByOrderInfo(String orderNum) {
        QueryWrapper<OrderBean> qwO = new QueryWrapper<>();
        qwO.eq("orderNum",orderNum);
        OrderBean orderBean = orderMapper.selectOne(qwO);
        if (orderBean.getStatus().equals("已支付")){
            QueryWrapper<OrderInfoBean> qwOinfo = new QueryWrapper<>();
            qwO.eq("fk_order",orderBean.getId());
            List<OrderInfoBean> orderInfoBeans = orderInfoMapper.selectList(qwOinfo);
            for (OrderInfoBean orderInfoBean : orderInfoBeans){
                QueryWrapper<WaitProductBean> qwW = new QueryWrapper<>();
                qwW.eq("productName",orderInfoBean.getCommodityName());
                WaitProductBean waitProductBean = waitProductDao.selectOne(qwW);
                if (waitProductBean.getNum()<orderInfoBean.getCommodityNum()){
                    return false;
                }
                waitProductBean.setNum(waitProductBean.getNum()-orderInfoBean.getCommodityNum());
                waitProductDao.updateById(waitProductBean);

            }
        }
        return true;
    }

    @Override
    public List<WaitProductDtoWeb> findWaitProductByTypeAndName(String type, String name) {
        QueryWrapper<WaitProductBean> qw = new QueryWrapper<>();
        qw.like("productName", "%" + name + "%");
        qw.like("productType", "%" + type + "%");
        List<WaitProductBean> waitProductBeans = waitProductDao.selectList(qw);
        return ConvertBeanUtil.copyList(waitProductBeans, WaitProductDtoWeb.class
           );
    }








}
